package org.testmyapp;

import java.io.IOException;
import java.io.PrintWriter;

import javax.inject.Inject;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.testmyapp.engine.IEngineServices;
import org.testmyapp.entities.TaskWorkerBean;
import org.testmyapp.persistence.ConfigObjectPersistenceProvider;

public class UserGroupSetupServlet extends HttpServlet implements Servlet {

	@Inject
	private IEngineServices engineServices;
	
	@Inject
	private ConfigObjectPersistenceProvider copp;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html");
		PrintWriter pw = resp.getWriter();
		pw.println("<h1>User & Group Setup</h1><br><br>");

		IdentityService identityService = engineServices.getIdentityService();

		Group staffFromDb = identityService.createGroupQuery()
				.groupName("staff").singleResult();

		Group staff = null;
		if (staffFromDb == null) {
			staff = identityService.newGroup("staff");
			staff.setName("staff");
			staff.setType("employee");
			identityService.saveGroup(staff);
			printGroup(pw, staff);
		}

		User jdFromDb = identityService.createUserQuery().userId("JD")
				.singleResult();
		User jd = null;
		if (jdFromDb == null) {
			jd = identityService.newUser("JD");
			jd.setFirstName("John");
			jd.setLastName("Dorian");
			jd.setPassword("dracula");
			identityService.saveUser(jd);
			printUser(pw, jd);
		}

		if (jd != null && staff != null) {
			identityService.createMembership(jd.getId(), staff.getId());
		}

		Group managementFromDb = identityService.createGroupQuery()
				.groupName("management").singleResult();

		Group management = null;
		if (managementFromDb == null) {
			management = identityService.newGroup("management");
			management.setName("management");
			management.setType("employee");
			identityService.saveGroup(management);
			printGroup(pw, management);
		}

		User bkFromDb = identityService.createUserQuery().userId("BK")
				.singleResult();

		User bk = null;
		if (bkFromDb == null) {
			bk = identityService.newUser("BK");
			bk.setFirstName("Bob");
			bk.setLastName("Kelso");
			bk.setPassword("bahamamama");
			identityService.saveUser(bk);
			printUser(pw, bk);
		}

		if (bk != null && management != null) {
			identityService.createMembership(bk.getId(), management.getId());
		}
		
		TaskWorkerBean twb = new TaskWorkerBean();
		twb.setBean("userTaskOne");
		twb.setProcessDefinitionId("process1");
		twb.setTaskDefinitionId("usertask1");
		copp.persist(twb);
		
		
	}

	private void printUser(PrintWriter pw, User user) {
		pw.println("Created User: " + user.getId() + "[name: "
				+ user.getFirstName() + " " + user.getLastName()
				+ ", password: " + user.getPassword() + "]<br>");
	}

	private void printGroup(PrintWriter pw, Group group) {
		pw.println("Created Group: " + group.getId() + "[name: "
				+ group.getName() + ", type: " + group.getType() + "]<br>");
	}

}
